import fetch from './request.js'
import api from '../api'
export default {
	//完单邀请生成任务
	release_task_invitation: async function(id, startTime = '', endTime = '', number = '') {
		let res = await fetch(api.release_task_invitation, {
			taskId: id,
			st: startTime,
			et: endTime,
			userNumber: number,
			number: number
		}, 'POST')
		if (res.code) {
			uni.showToast({
				title: res.msg,
				icon: 'none'
			})
			return false
		} else {
			return res.data
		}
	},
	//邀请
	invitation_order: async function(list, taskid, oldId) {
		list = Array.isArray(list) ? list : list + ''
		if ((Array.isArray(list) && list.length != 0) || (typeof(list) == 'string') && list != '') {
			let ids = typeof(list) == 'string' ? list : list.join(',')
			let res = await fetch(api.invitation_order, {
				taskId: taskid,
				userIds: ids,
			}, 'POST')
			if (res.code && (!Array.isArray(list))) {
				uni.showToast({
					title: res.msg,
					icon: 'none'
				})
			}
			let _staffid = Array.isArray(list) ? list : ids
			this.invitation_order_Storage(_staffid, oldId)
		} else {
			uni.showToast({
				title: '请选择邀请对象',
				icon: 'none'
			})
		}
	},
	//存储被邀请的id
	invitation_order_Storage: function(list, taskid) {
		list = Array.isArray(list) ? list : [parseInt(list)]
		let key = 'id' + taskid
		let invitation_order = uni.getStorageSync('invitation_order') ? uni.getStorageSync('invitation_order') : {}
		let idList = Object.keys(invitation_order).includes(key) ? invitation_order[key] : []
		idList = Array.isArray(idList) ? idList : []
		if (idList.includes(list)) return
		idList = idList.concat(list)
		invitation_order[key] = idList
		uni.setStorageSync('invitation_order', invitation_order)
	},
	order_evaluate: async function(id, score, text, textInfo, type) {
		//score 2  4 5
		let evaluate = {}
		let title = score == '2' && type == 0 ? '评价成功，用户已加入黑名单' : '评价成功'
		evaluate.orderId = id
		evaluate.anonymous = score == '2' ? 1 : 0
		evaluate.score = score
		evaluate.appraise = textInfo == '' ? text : textInfo
		evaluate.type = type
		let res = await fetch(api.order_evaluate, evaluate, 'POST', 'JSON')
		if (res.code) return uni.showToast({
			title: res.msg,
			icon: 'none'
		})
		uni.showToast({
			title: title,
			icon: 'none'
		})
	},
	// 获取诚信值
	my_flows: async function(hasFlag, requestObj, honestList) {
		if (!hasFlag) return [honestList, hasFlag] // 说明已经没有更多啦
		let res = await fetch(api.my_flow, requestObj, 'POST')
		if (res.code) uni.showToast({
			title: res.msg,
			icon: 'none'
		})
		// 整理数据
		res.rows.forEach(item => {
			item.amount = (item.amount / 100).toFixed(2)
		})
		// 拼接
		honestList = [...honestList, ...res.rows]
		hasFlag = honestList.length < res.total
		return [honestList, hasFlag]
	},
	attract_business: async function(checkCode, city, county, province, telephone, urname) {
		let res = await fetch(api.attract_business, {
			'checkCode': checkCode,
			'city': city,
			'county': county,
			'province': province,
			'telephone': telephone,
			'urname': urname
		}, 'GET')
		return res
	},
	get_Open_area: function(region) {
		let adcode = uni.getStorageSync('property_list_by_type').adcode.split(',')
		for (var i = 0; i < adcode.length; i++) {
			if (adcode[i] == region.slice(0, 6)) {
				return true
			}
		}
		return false
	},
	getCity: function() {
		let location = uni.getStorageSync('location')
		return new Promise((resolve, reject) => {
			uni.request({
				url: 'https://restapi.amap.com/v3/geocode/regeo?parameters',
				data: {
					key: 'd73cc951a96637a251a9a2446b48ce9c',
					location: location[1] + ',' + location[0]
				},
				success: res => {
					let city = res.data.regeocode.addressComponent.city.length != 0 ? res.data
						.regeocode.addressComponent.city : res.data.regeocode.addressComponent
						.province
					resolve(city)
				}
			})
		})
	},
	attract_business_size: async function(city) {
		let res = await fetch(api.attract_business_size, {
			county: city
		}, 'GET')
		//res.data
		return res
	},
	get_task_Latitude: async function() {
		let res = await fetch(api.get_task_Latitude, {}, 'POST')
		//res.msg
		return res.msg
	},
	postOrder_senddown: async function(orderId, senddown) {
		let res = await fetch(api.order_senddown, {
			orderId: orderId,
			senddown: senddown,
		}, 'POST')
		return res
	},
	//添加订单举报理由resion理由/type类型/orderId订单id
	order_report_id: async function(orderId, resion, type) {
		let res = await fetch(api.order_report_id, {
			orderId: orderId,
			resion: resion,
			type: type
		}, 'POST')
		return res
	},
	Closure_time: async function() {
		let res = await fetch(api.Closure_time, {}, 'POST')
		if (res.data != 0) {
			uni.setStorageSync('FengjinItem', res.data)
			uni.setStorageSync('jf_expireTime', res.data.expireTime)
		} else if (res.data == 0) {
			try {
				uni.removeStorageSync('FengjinItem')
			} catch (e) {}
		}
		return res
	},
	Closure_margin: async function() {
		let res = await fetch(api.Closure_margin, {}, 'POST')
		if (res.data != 0) {
			uni.setStorageSync('yajinItem', res.data)
		} else if (res.data == 0) {
			try {
				uni.removeStorageSync('yajinItem')
			} catch (e) {
				// error
			}
		}
		return res
	},
	Closure_need_margin: async function(UnsealId) {
		let res = await fetch(api.Closure_need_margin, {
			UnsealId: UnsealId
		}, 'POST')
		uni.showToast({
			title: res.msg,
			icon: 'none'
		})
		return res
	},
	upd_user_banner: async function(imageurl) {
		let res = await fetch(api.upd_user_banner, {
			appurl: imageurl
		}, 'POST')
		return res
	},
}